package echat.future.cn.echat.message.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.message.bean.EbiBean;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.CircleImageView;

/**
 * 我的E币
 * Created by JX on 2018/8/13.
 */

public class EbiAdapter extends BaseQuickAdapter<EbiBean, BaseViewHolder> {

    /**
     * create_time: 操作时间；
     * e_money：E币数（负数表示消费，正数表示入帐）；
     * type：E币类型[充值：1，平台调整：2，视频消费：3，打赏：4，提现：5，下级消费奖励：6]；
     * pay_type：支付方式【1：支付宝，2：微信为，为空则表示非充值】
     */

    public EbiAdapter(int layoutResId, @Nullable List<EbiBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, EbiBean item) {
        //E币为正数
        boolean isAdd = item.getE_money() > 0;

        helper.setText(R.id.tv_time, getTime(item) + "：" + transferLongToDate(item.getCreate_time() * 1000));

        helper.setText(R.id.tv_money_type, getType(item) + "：");

        //认证
//        helper.setGone(R.id.iv_renzheng, item.getType() == 1 ? false : true);
        // 皇冠
//        helper.setGone(R.id.iv_guanyin, item.getType() == 1 ? false : true);
        //级别
        helper.setGone(R.id.tv_jibie, item.getType() == 1 ? false : true);
        //通话时长
        helper.setGone(R.id.tv_call, item.getPay_type() != 0 ? false : true);
        helper.setText(R.id.tv_call, getCall(item));

        if (item.getPay_type() != 0) {
            if (item.getPay_type() == 1) {
                helper.setText(R.id.tv_name, "支付宝充值");
                helper.setImageResource(R.id.iv_head, R.drawable.ebi_zfb);
            } else {
                helper.setText(R.id.tv_name, "微信充值");
                helper.setImageResource(R.id.iv_head, R.drawable.ebi_wx);
            }
        } else {
            //设置用户信息
            String level = UserHelper.getUser().getLevel();
            helper.setText(R.id.tv_jibie, TextUtils.isEmpty(level) ? "" : "LV." + level);
            helper.setGone(R.id.tv_jibie, TextUtils.isEmpty(level) ? false : true);
            helper.setText(R.id.tv_name, UserHelper.getUser().getUsername());
            RequestOptions options = new RequestOptions();
            options.centerCrop()
                    .placeholder(R.mipmap.ic_launcher);
            Glide.with(mContext).load(UserHelper.getUser().getAvatar()).apply(options).into((CircleImageView) helper.getView(R.id.iv_head));
        }

        helper.setTextColor(R.id.tv_money, Color.parseColor(isAdd ? "#3CB034" : "#F6345E"));
        helper.setText(R.id.tv_money, item.getE_money() + "E币");
    }

    private String getTime(EbiBean item) {
        if (item.getType() == 1) {
            return "充值日期";
        }
        if (item.getE_money() > 0) {
            return "直播日期";
        }
        return "消费日期";
    }

    private String getCall(EbiBean item) {
        if (item.getType() == 2) {
            return "平台调整";
        } else if (item.getType() == 3) {
            return "视频消费";
        } else if (item.getType() == 4) {
            return "打赏";
        } else if (item.getType() == 5) {
            return "提现";
        } else if (item.getType() == 6) {
            return "下级消费奖励";
        }
        return "";
    }

    private String getType(EbiBean item) {

        if (item.getPay_type() != 0) {
            return "充值到账";
        }
        if (item.getE_money() > 0) {
            return "直播收入";
        }
        return "消费支出";
    }

    /**
     * 把long 转换成 日期 再转换成String类型
     */
    public String transferLongToDate(Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        Date date = new Date(millSec);
        return sdf.format(date);
    }

}
